import 'dart:io';

import 'package:flutter/material.dart';

import 'dart:ffi';
import 'ffi/rust_ffi.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
        useMaterial3: true,
      ),
      home: const MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key, required this.title});

  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  Future<String>? hello;

  @override
  void initState() {
    super.initState();
    if (Platform.isAndroid) {
      hello = NativeImpl(DynamicLibrary.open("libnative.so")).hello();
    } else if (Platform.isIOS) {
      hello = NativeImpl(DynamicLibrary.process()).hello();
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Theme.of(context).colorScheme.inversePrimary,
        title: Text(widget.title),
      ),
      body: Center(
        child: hello != null
            ? FutureBuilder<List<dynamic>>(
                future: Future.wait([hello!]),
                builder: (context, snap) {
                  final data = snap.data;
                  if (data == null) {
                    return const CircularProgressIndicator(); // ignore: non_constant_identifier_names
                  }
                  final text = data[0];
                  return Text('$text');
                },
              )
            : const SizedBox(),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () {},
        tooltip: 'Increment',
        child: const Icon(Icons.add),
      ), // This trailing comma makes auto-formatting nicer for build methods.
    );
  }
}
